package com.xs.boot.controller.kaosheng;

import com.xs.boot.dao.kaosheng.PersonalRegistMapper;
import com.xs.boot.entity.kaosheng.DistrictEntity;
import com.xs.boot.entity.kaosheng.MajorEntity;
import com.xs.boot.entity.kaosheng.StudentInfoEntity;
import com.xs.boot.entity.kaosheng.ZhuKaoSchoolEntity;
import com.xs.boot.service.impl.KaoShengService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by FF on 2020/6/22.
 */
@Controller
@RequestMapping("/kaosheng")
public class KaoShengController {
    @Autowired
    private KaoShengService kaoShengService;

    @RequestMapping("/goto-personal-regist")
    public String gotoPersonalRegist() {
        return "kaosheng/personal-regist";
    }


    @RequestMapping(value = "/personal-regist", method = RequestMethod.POST)
    @ResponseBody
    //返回动态构造的json数据
    public String personalRegist(@RequestBody StudentInfoEntity studentInfoEntity) {
        //System.out.println("前端传入的数据：" + studentInfoEntity);
        int flag = kaoShengService.insert(studentInfoEntity);
        //System.out.println("插入标志："+flag);

        //构建准考证
        String szNum = studentInfoEntity.getSzNum();
        String qxNum = studentInfoEntity.getQxNum();
        //当前时间
        Date date = new Date( );
        SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
        String dateNow = sdf.format(date);
        //System.out.println("当前时间为: " + dateNow);
        String year = dateNow.substring(2,4);
        int numThisYear = 1;
        String studentId = studentInfoEntity.getStudentId() + "";
        if (studentId.length() != 5) {
            int makeUpLength = 5 - studentId.length();
            for (int i = 0; i < makeUpLength; i++) {
                studentId = "0"+studentId;
            }
        }
        String zkaoNum = szNum+qxNum+year+numThisYear+studentId;
        //System.out.println("准考证："+zkaoNum);
        //设置准考证号
        studentInfoEntity.setZkaoNum(zkaoNum);

        //设置考生状态
        studentInfoEntity.setBmState(0);

        //设置操作时间
        studentInfoEntity.setOpTime(dateNow);

        //更新数据库
        if (flag > 0){
            //System.out.println("更新前:"+studentInfoEntity);
            flag = kaoShengService.updateStudentInfo(studentInfoEntity);
            //System.out.println("更新标志："+flag);
        }

        // 动态构建json
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("flag", flag);
        List<StudentInfoEntity> studentInfoEntityList = new ArrayList<>();
        studentInfoEntityList.add(studentInfoEntity);
        jsonObject.put("data", studentInfoEntityList);
        return jsonObject.toString();
    }

    @RequestMapping(value = "/getAllMajors", method = RequestMethod.GET)
    @ResponseBody
    public String getAllMajors() {
        List<MajorEntity> majorEntityList = kaoShengService.getAllMajors();

        int flag = 0;
        if (majorEntityList.size() > 0) {
            flag = 1;
        }

        // 动态构建json
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("flag", flag);
        jsonObject.put("data", majorEntityList);
        //System.out.println(jsonObject);
        return jsonObject.toString();
    }

    @RequestMapping(value = "/getAllCities", method = RequestMethod.GET)
    @ResponseBody
    public String getAllCities() {
        List<DistrictEntity> districtEntityList = kaoShengService.getAllCities();

        int flag = 0;
        if (districtEntityList.size() > 0) {
            flag = 1;
        }

        // 动态构建json
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("flag", flag);
        jsonObject.put("data", districtEntityList);
        //System.out.println(jsonObject);
        return jsonObject.toString();
    }

    @RequestMapping(value = "/getAllDistricts/{cityCode}", method = RequestMethod.POST)
    @ResponseBody
    public String getAllDistricts(@PathVariable("cityCode") String cityCode) {
        //System.out.println("输出citycode:" + cityCode);
        List<DistrictEntity> districtEntityList = kaoShengService.getAllDistricts(cityCode);

        int flag = 0;
        if (districtEntityList.size() > 0) {
            flag = 1;
        }

        // 动态构建json
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("flag", flag);
        jsonObject.put("data", districtEntityList);
        //System.out.println(jsonObject);
        return jsonObject.toString();
    }

    @RequestMapping(value = "/getAllZhuKaoSchools", method = RequestMethod.GET)
    @ResponseBody
    public String getAllZhuKaoSchools() {
        List<ZhuKaoSchoolEntity> zhuKaoSchoolEntityList = kaoShengService.getAllZhuKaoSchools();

        int flag = 0;
        if (zhuKaoSchoolEntityList.size() > 0) {
            flag = 1;
        }

        // 动态构建json
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("flag", flag);
        jsonObject.put("data", zhuKaoSchoolEntityList);
        //System.out.println(jsonObject);
        return jsonObject.toString();
    }
}
